clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output

// open visit dataset
use clean/radiology_visit_3.dta, clear


// Make treatment indicator
gen post = (ym>=ym(2007,3))
gen web_post = (visit_on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"

// Short-run and long-run treatment indicator
gen web_post_sr= web_post
replace web_post_sr = 0 if ym>ym(2009,3)
gen web_post_lr= web_post
replace web_post_lr = 0 if ym<=ym(2009,3)
label var web_post_sr "$\text{OnWeb}\textsubscript{m}\times\text{PostShortRun}\textsubscript{t}$"
label var web_post_lr "$\text{OnWeb}\textsubscript{m}\times\text{PostLongRun}\textsubscript{t}$"


global ctr_vars1 = "age male i.charlindex zip_mean_income zip_pct_ba i.ym i.std_product_type i.payer_id"
global ctr_vars2 = "age male i.charlindex zip_mean_income zip_pct_ba i.ym i.std_product_type"


// By HHI
// Merge on county
rename member_zip zip
merge m:1 zip using dist/zcta_county_rel_10.dta
drop if _merge==2
rename zip member_zip
rename county serv_prov_county
drop _merge

// Merge hhi
merge m:1 serv_prov_county proc_class using clean/hhi.dta
summ hhi total_visits
mdesc hhi total_visits

egen q4_hhi = xtile(hhi), by(proc_class) n(4)
gen low_hhi = q4_hhi==1 & hhi~=. 
gen high_hhi = q4_hhi==4 & hhi~=. 
	
	
egen prov_proc_fe = group(proc_id_radio prov_id payer_id)


areg ln_amt_allowed web_post_sr web_post_lr $ctr_vars2, absorb(prov_proc_fe) vce(cl ym)
summ amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a1


areg ln_amt_allowed_radio web_post_sr web_post_lr $ctr_vars2, absorb(prov_proc_fe) vce(cl ym)
summ amt_allowed_radio if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a2

// By HHI

areg ln_amt_allowed web_post_sr web_post_lr $ctr_vars2 if q4_hhi==1 & hhi~=. , absorb(prov_proc_fe) vce(cl ym)
summ ln_amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a3

areg ln_amt_allowed web_post_sr web_post_lr $ctr_vars2 if q4_hhi==4 & hhi~=. , absorb(prov_proc_fe) vce(cl ym) 
summ ln_amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a4


// By Provider type
gen prov_hospital_tmp = (ub_facility==1)
bys prov_id: egen prov_hospital_share = mean(prov_hospital_tmp)
gen prov_hospital = (prov_hospital_share>0.25)
drop prov_hospital_tmp prov_hospital_share

areg ln_amt_allowed web_post_sr web_post_lr $ctr_vars2 if prov_hospital==1, absorb(prov_proc_fe) vce(cl ym) 
summ ln_amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a5

areg ln_amt_allowed web_post_sr web_post_lr $ctr_vars2 if prov_hospital==0, absorb(prov_proc_fe) vce(cl ym) 
summ ln_amt_allowed if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a6

esttab a1 a2 a3 a4 a5 a6 using $output/supply_side_regs_a.tex, frag cells(b(fmt(3) star) se(fmt(3) par)) ///
	keep(web_post_sr web_post_lr )  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 

esttab a1 a2 a3 a4 a5 a6 using $output/supply_side_regs_b.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp ar2 N, fmt(2 3 %12.0fc) labels("Mean level" "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 


////////////////
// Wald Test
////////////////

// Blank
quiet reg ln_amt_allowed web_post
estadd local Fstat_sr=" \:"
estadd local Fstat_lr=" \:"
eststo t1

preserve
gen strat_group = 1 if low_hhi==1
replace strat_group = 0 if high_hhi==1
drop if strat_group==.

egen strat_prov_proc_fe = group(prov_proc_fe strat_group)
egen ym_strat = group(ym strat_group)

gen web_post_sr_strat1 = 0
replace web_post_sr_strat1 = web_post_sr if strat_group==1
gen web_post_sr_strat2 = 0
replace web_post_sr_strat2 = web_post_sr if strat_group==0
gen web_post_lr_strat1 = 0
replace web_post_lr_strat1 = web_post_lr if strat_group==1
gen web_post_lr_strat2 = 0
replace web_post_lr_strat2 = web_post_lr if strat_group==0


quiet areg ln_amt_allowed web_post_sr_strat1 web_post_sr_strat2 web_post_lr_strat1 web_post_lr_strat2 c.age#strat_group c.male#strat_group i.charlindex#i.strat_group c.zip_mean_income#strat_group c.zip_pct_ba#i.strat_group i.ym#i.strat_group i.std_product_type#i.strat_group, absorb(strat_prov_proc_fe) vce(cl ym_strat)  

test web_post_sr_strat1= web_post_sr_strat2 
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_sr=string(`rF',"%04.3f")+"`stars'"

test web_post_lr_strat1= web_post_lr_strat2 
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_lr=string(`rF',"%04.3f")+"`stars'"
eststo t2
restore


// Hospital vs non-hospital, with prov FEs
preserve
gen strat_group = 1 if prov_hospital==1
replace strat_group = 0 if prov_hospital==0
drop if strat_group==.

egen strat_prov_proc_fe = group(prov_proc_fe strat_group)
egen ym_strat = group(ym strat_group)

gen web_post_sr_strat1 = 0
replace web_post_sr_strat1 = web_post_sr if strat_group==1
gen web_post_sr_strat2 = 0
replace web_post_sr_strat2 = web_post_sr if strat_group==0
gen web_post_lr_strat1 = 0
replace web_post_lr_strat1 = web_post_lr if strat_group==1
gen web_post_lr_strat2 = 0
replace web_post_lr_strat2 = web_post_lr if strat_group==0


quiet areg ln_amt_allowed web_post_sr_strat1 web_post_sr_strat2 web_post_lr_strat1 web_post_lr_strat2 c.age#strat_group c.male#strat_group i.charlindex#i.strat_group c.zip_mean_income#strat_group c.zip_pct_ba#i.strat_group i.ym#i.strat_group i.std_product_type#i.strat_group, absorb(strat_prov_proc_fe) vce(cl ym_strat)  

test web_post_sr_strat1= web_post_sr_strat2 
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_sr=string(`rF',"%04.3f")+"`stars'"

test web_post_lr_strat1= web_post_lr_strat2 
local rF = r(F)
local stars = ""
local rp = r(p)
if `rp'<0.1 local stars = "\sym{*}"
if `rp'<0.05 local  stars = "\sym{**}"
if `rp'<0.01 local  stars = "\sym{***}"
estadd local Fstat_lr=string(`rF',"%04.3f")+"`stars'"
eststo t3
restore

// F-Stat Only
esttab t1 t2 t3 using $output/supply_side_regs_c.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(Fstat_sr Fstat_lr, fmt(3 3) labels("\textit{F} statistic of diff. (SR)" "\textit{F} statistic of diff. (LR)") layout("\multicolumn{2}{c}{@}" "\multicolumn{2}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 

